<?php
/**
 *
 * [2012/03/25][Milo Liu] Add following features in 1.3 branch
 * 1. Allow set the master/slaver type for a DB connection
 * 2. add the charater setting into the configuration of each DB connection
 *
 * Move the code for the class Dao to a new file Dao.class.php
 * Move the code for the class Value to a new file Value.class.php
 */
require_once('pworks/common/dao/DBConfig.class.php');
require_once('pworks/common/dao/Dao.class.php');
require_once('pworks/common/dao/Value.class.php');

abstract class DaoFactory {
    private static $_pdos = null;
    private static $_log = null;

    private static $_debugMode = false;

    public static function setDebugMode($flag){
        if($flag) self::$_debugMode = true;
        else self::$_debugMode = false;
    }

    private static function getPdo($dbConfigKey) {
        if(array_key_exists($dbConfigKey, self::$_pdos)){
            return self::$_pdos[$dbConfigKey];
        }

        if(self::$_debugMode){
            if(!DBConfig::check($dbConfigKey)){
                die(DBConfig::getErrorMessage());
            }
        }

        $dbConfig = DBConfig::getDBConfig($dbConfigKey);


        try{
            $mDbConfig = $dbConfig[DB_LB_MASTER][0];
            self::$_pdos[$dbConfigKey][DB_LB_MASTER] = new PDO(self::getMysqlDSN($mDbConfig) 
                , $mDbConfig["DB_USER"]
                , $mDbConfig["DB_PSW"]);
            self::$_pdos[$dbConfigKey][DB_LB_MASTER]->exec("set names ". $mDbConfig['DB_CHARSET']);
        }catch(PDOException $e){
            self::log('Connection of the Master Database Failed: '. $e->getMessage());
            die('Connection of the Master Database Failed: '. $e->getMessage());
        }


        foreach($dbConfig[DB_LB_SLAVER] as $sDbKey => $sDbConf){
            try{
                self::$_pdos[$dbConfigKey][DB_LB_SLAVER][] = new PDO(self::getMysqlDSN($sDbConf)
                    , $sDbConf["DB_USER"]
                    , $sDbConf["DB_PSW"]);

                self::$_pdos[$dbConfigKey]->exec("set names " . $sDbConf['DB_CHARSET']);
            }catch(PDOException $e){
                self::log('Connection of the No. '. $sDbKey  . '  Slaver Database Failed: '. $e->getMessage());
            }

        }
        return self::$_pdos[$dbConfigKey];
    }

    public static function log($errorMessage){
        if(self::$_debugMode){
            echo($errorMessage);
            echo "\n";
        }
    }

    private static function getMysqlDSN($config){
        return 'mysql:host='.$config["DB_HOST"].';dbname='.$config["DB_NAME"];
    }


    /**
     * Rewrite to pass the pdo array into the Dao construct method
     * So that all database operation will be wrapped into DAO, 
     * And there will no way to get the pdo object to call the PDO's origial method 
     */
    // Backup of v1.2
    // ---------------
    //public static function getDao($table, $dbConfigKey="default") {
    //    return new Dao($table, $dbConfigKey);
    //}
    //---------------
    public static function getDao($table, $dbConfigKey="default") {
        $dao = new Dao($table, self::getPdo($dbConfigKey));
        $dao->init();
        return $dao;
    }

    public static function close($dbConfigKey) {
        self::$_pdos[$dbConfigKey] = null;
    }
}
